package com.dmooo.yxmmerchant.view.ui;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.common.util.CheckUtil;
import com.common.util.ToastUtil;
import com.cretin.www.clearedittext.view.ClearEditText;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.view.contract.ChangePhonePwdContract;
import com.dmooo.yxmmerchant.view.presenter.ChangePhonePwdPreseter;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPwdActivity extends BaseActivity<ChangePhonePwdPreseter> implements ChangePhonePwdContract.ChangeView {
    @BindView(R.id.tv_title)
    TextView txtTitle;
    @BindView(R.id.txt_tip)
    TextView txtTip;
    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.et_code)
    ClearEditText etCode;
    @BindView(R.id.txt_get_code)
    TextView txtGetCode;
    @BindView(R.id.et_new_pwd_one)
    ClearEditText etNewPwdOne;
    TimeCount time;
    @Override
    protected int getLayout() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void initEventAndData() {
        txtTitle.setText("忘记密码");
        txtTip.setVisibility(View.GONE);
        txtTip.setSelected(true);
        time = new TimeCount(60000, 1000);
    }

    @Override
    protected void createPresenter() {
        mPresenter=new ChangePhonePwdPreseter(this,this);
    }
    @OnClick({R.id.tv_left, R.id.txt_get_code, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.txt_get_code:
                if(!CheckUtil.isMobile(etPhone.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入手机号");
                    return;
                }
                mPresenter.getResetPhoneCode(etPhone.getText().toString());
                break;
            case R.id.btn_ok:
                if(TextUtils.isEmpty(etCode.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入验证码");
                    return;
                }
                if(TextUtils.isEmpty(etNewPwdOne.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入新密码");
                    return;
                }
                mPresenter.resetPhonePwd(etPhone.getText().toString(),etCode.getText().toString(),etNewPwdOne.getText().toString());
                break;
        }
    }

    @Override
    public void sendOldPhoneSuccess() {
   }

    @Override
    public void sendNewPhoneSuccess() {
        ToastUtil.getInstance().showSuccessMsg("验证码发送成功");
        time.start();
        txtTip.setVisibility(View.VISIBLE);
        txtTip.setSelected(true);
        txtTip.setText("验证码已发送至手机号"+ etPhone.getText().toString()+"，请注意查收！");


    }

    @Override
    public void changePhoneSuccess() {

    }

    @Override
    public void changePwdSuccess() {

    }

    @Override
    public void resetSccess() {
        ToastUtil.getInstance().showSuccessMsg("密码重置成功");
        finish();
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @SuppressLint("NewApi")
        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            txtGetCode.setClickable(false);
            txtGetCode.setText("倒计时" + millisUntilFinished / 1000 + "s");
            txtGetCode.setTextColor(Color.WHITE);
            txtGetCode.setBackground(getDrawable(R.drawable.shape_phone_code_unenable));
        }

        @SuppressLint("NewApi")
        @Override
        public void onFinish() {// 计时完毕时触发
            txtGetCode.setText("获取验证码");
            txtGetCode.setClickable(true);
            txtGetCode.setTextColor(getResources().getColor(R.color.white));
            txtGetCode.setBackground(getDrawable(R.drawable.shape_phone_code));
        }

    }
}
